package com.nj.api.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nj.api.entity.Income;
import com.nj.api.mapper.AccountMapper;
import com.nj.api.mapper.IncomeMapper;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;

@Service
public class IncomeServiceImpl implements IncomeService {
    @Resource
    private IncomeMapper incomeMapper;
    @Resource
    private AccountMapper accountMapper;
    @Override//自动返现定时任务
    @Scheduled(cron = "0 0 2 * * ?")//代表0秒0分2时，每月的每一天，每年的每一月，每周的？天，这个表达式必须要有一个?，且第三个和第五个为互斥
    public void returnMoney() {
        //查询所有返现状态为0（未返现），并且返现日期在当前日期或这钱的日期
        LocalDate now = LocalDate.now();
        QueryWrapper<Income> wrapper = new QueryWrapper<Income>().eq("income_status", 0).le("income_date",now);
        List<Income> incomes = incomeMapper.selectList(wrapper);
        if (incomes==null || incomes.size()==0) return;
        for (Income income : incomes) {
           BigDecimal incr =  income.getBidMoney().add(income.getIncomeMoney());
           accountMapper.increaseMoney(income.getUid(),incr);//收益加到用户账户里面
           income.setIncomeStatus(1);
           incomeMapper.updateById(income);//更改返现状态为已返现（1）
        }
    }
}
